package common.converter;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author yuanqinglong
 * @since 2021/3/19 16:42
 */
public class CostValueValidator implements ConstraintValidator<CostValue, String> {


    private volatile Set<String> values = new HashSet<>();

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        return values.contains(value);
    }


    @Override
    public void initialize(CostValue constraintAnnotation) {
        synchronized (CostValueValidator.class) {
            values = new HashSet<>(Arrays.asList(constraintAnnotation.values()));
        }
    }
}
